//
//  GalleryDetailVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/4/24.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "GalleryDetailVC.h"
#import "LotteryTypeView.h"
#import "ImageCell.h"
#import "InformationVM.h"
#import "LabCell.h"
#import "GalleryDetailTopCell.h"
#import "LotteryHeadTitleView.h"

@interface GalleryDetailVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
@property (nonatomic ,strong) LotteryTypeView *lotteryTypeView;
@property (nonatomic ,strong) UICollectionView *bottomCollectionView;
@property (nonatomic ,strong) NSMutableArray    *dataArr;
@property (nonatomic ,strong) NSMutableArray    *recommendArr;
@property (nonatomic ,assign)NSInteger   picHeight;
@property (nonatomic ,assign)NSInteger   scrollIndex;

@end

@implementation GalleryDetailVC{

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getGalleryDetail];
    
    // Do any additional setup after loading the view.
}

- (void)backClick{
    [self.navigationController popToViewController:self.saveViewController animated:YES];
}

- (void)setId:(NSString *)Id{
    _Id=Id;
    
}

///获取图库详情
- (void)getGalleryDetail{
    WS(weakSelf);
    [[InformationVM manage] getGrlleryDetailWithId:self.Id Success:^(id returnValue) {
        weakSelf.dataArr=returnValue;
        [weakSelf.lotteryTypeView setDataArr:returnValue];
        [weakSelf.bottomCollectionView reloadData];
        [weakSelf getRecommendGrllery];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

///获取推荐图库
- (void)getRecommendGrllery{
    WS(weakSelf);
    [[InformationVM manage] getRecommendGrlleryWithId:self.Id Success:^(id returnValue) {
        weakSelf.recommendArr=returnValue;
        [weakSelf.bottomCollectionView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

#pragma mark - collectionView代理函数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }
    return self.recommendArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return CGSizeMake(SCREEN_WIDTH,self.picHeight>0?self.picHeight+40:SCREEN_HEIGHT);
    }
    return CGSizeMake(SCREEN_WIDTH/2.0, 40);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        GalleryDetailTopCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"GalleryDetailTopCell" forIndexPath:indexPath];
        if (cell.index!=self.lotteryTypeView.selectLottery) {
            cell.index=self.lotteryTypeView.selectLottery;
        }
        WS(weakSelf);
        cell.changeHeight = ^(NSInteger height) {
            if (weakSelf.picHeight==0) {
                weakSelf.picHeight=height;
                [weakSelf.bottomCollectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
            }
        };
        cell.locationChange = ^(NSInteger index) {
            weakSelf.scrollIndex=index;
            if (weakSelf.lotteryTypeView.selectLottery!=index) {
                weakSelf.lotteryTypeView.selectLottery=index;
                [weakSelf.lotteryTypeView setScrollIndex:index];
            }
        };
        cell.dataArr=self.dataArr;
        return cell;
    }
    LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
    GalleryExtraModel *model=[self.recommendArr objectAtIndex:indexPath.row];
    [cell setLeftTitle:model.Name andLeft:10];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView==self.bottomCollectionView) {
        if (indexPath.section==0) {
            return;
        }
        GalleryExtraModel *model=[self.recommendArr objectAtIndex:indexPath.row];
        GalleryDetailVC *vc=[[GalleryDetailVC alloc]init];
        vc.title=model.Name;
        vc.Id=model.Id;
        vc.saveViewController=self.saveViewController;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        CGSize size = CGSizeMake(0, 0);
        return size;
    }
    CGSize size = CGSizeMake(SCREEN_WIDTH, 40);
    return size;
}

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader)
    {
        LotteryHeadTitleView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LotteryHeadTitleView" forIndexPath:indexPath];
        
        
        headerView.isHiddenBtnView = YES;
        [headerView setTitle:@"其他推荐"];
        headerView.backgroundColor=DEFAULTBGCOLOR;
        return headerView;
    }else{
        UICollectionReusableView *view = [[UICollectionReusableView alloc] initWithFrame:CGRectZero];
        return view;
    }
    
}

#pragma mark -- 懒加载
- (LotteryTypeView *)lotteryTypeView{
    if (!_lotteryTypeView) {
        WS(weakSelf);
        _lotteryTypeView=[[LotteryTypeView alloc]init];
        _lotteryTypeView.chaneBlock = ^(NSInteger row) {
            if (row!=weakSelf.scrollIndex) {
                [weakSelf.bottomCollectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
            }else{
                weakSelf.scrollIndex=-1;
            }
        };
        [self.view addSubview:_lotteryTypeView];
        [_lotteryTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(weakSelf.view);
            make.height.mas_equalTo(@40);
        }];
    }
    return _lotteryTypeView;
}

- (UICollectionView *)bottomCollectionView{
    if (_bottomCollectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _bottomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _bottomCollectionView.delegate = self;
        _bottomCollectionView.dataSource = self;
        _bottomCollectionView.backgroundColor=[UIColor whiteColor];
        _bottomCollectionView.showsVerticalScrollIndicator=NO;
        [_bottomCollectionView registerClass:[GalleryDetailTopCell class] forCellWithReuseIdentifier:@"GalleryDetailTopCell"];
        [_bottomCollectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];
        [_bottomCollectionView registerClass:[LotteryHeadTitleView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LotteryHeadTitleView"];
        
        _bottomCollectionView.panGestureRecognizer.delaysTouchesBegan = _bottomCollectionView.delaysContentTouches;
        
        [self.view addSubview:_bottomCollectionView];
        [_bottomCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(self.view);
            make.top.equalTo(self.lotteryTypeView.mas_bottom);
        }];
    }
    return _bottomCollectionView;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
